/*
 * Copyright 2019 Flipkart Internet Pvt. Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flipkart.ohos.proteus.toolbox;

import com.flipkart.ohos.proteus.value.Array;
import com.flipkart.ohos.proteus.value.Null;
import com.flipkart.ohos.proteus.value.Primitive;
import com.flipkart.ohos.proteus.value.Value;

/**
 * Result class hold the return status and the data
 *
 * @author aditya.sharat
 */
public class Result {
    /**
     * singleton for No Such Data Path Exception.
     */
    public static final Result NO_SUCH_DATA_PATH_EXCEPTION = new Result(ResultCode.RESULT_NO_SUCH_DATA_PATH_EXCEPTION,
        Null.INSTANCE);

    /**
     * singleton for Invalid Data Path Exception.
     */
    public static final Result INVALID_DATA_PATH_EXCEPTION = new Result(ResultCode.RESULT_INVALID_DATA_PATH_EXCEPTION,
        Null.INSTANCE);

    /**
     * singleton for Null Exception.
     */
    public static final Result NULL_EXCEPTION = new Result(ResultCode.RESULT_NULL_EXCEPTION, Null.INSTANCE);

    /**
     * Indicates the return status of the method for a given data path. The return value
     * will be {@code RESULT_SUCCESS} if and only if the data path exists and contains
     * a valid {@link Value}.
     */

    public final ResultCode resultCode;
    /**
     * The value at the specified data path.
     * {@code value} will be null if {@code RESULT_CODE} != {@code RESULT_SUCCESS}
     */

    public final Value value;

    public Result(ResultCode resultCode, Value value) {
        this.resultCode = resultCode;
        this.value = value;
    }

    /**
     * This method return a {@link Result} object with {@code RESULT_CODE} == {@code RESULT_SUCCESS}
     * and {@code Result#value} == {@code value}.
     *
     * @param value The {@link Value} to be wrapped.
     * @return A {@link Result} object with with {@code RESULT_CODE} == {@code RESULT_SUCCESS}.
     */
    public static Result success(Value value) {
        return new Result(ResultCode.RESULT_SUCCESS, value);
    }

    /**
     * isSuccess
     *
     * @return true if and only if {@code RESULT_CODE} == {@code RESULT_SUCCESS}.
     */
    public boolean isSuccess() {
        return this.resultCode == ResultCode.RESULT_SUCCESS;
    }

    public enum ResultCode {
        /**
         * Indicates that the data path specified is invalid. As an example, looking for a
         * property inside a {@link Primitive} or
         * {@link Array}.
         */

        RESULT_INVALID_DATA_PATH_EXCEPTION,

        /**
         * Indicates that the object does not have the specified data path.
         */
        RESULT_NO_SUCH_DATA_PATH_EXCEPTION,

        /**
         * Indicates that a valid {@link Value} was found at the specified data path.
         */
        RESULT_SUCCESS,

        /**
         * Indicates that the data path prematurely led to a {@link Null}
         */
        RESULT_NULL_EXCEPTION;
    }
}